package org.iabako.client.ui.page.business.sale;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigSearchEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.ui.form.business.sale.SaleSearchForm;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.client.ui.page.AbstractSearchPage;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.enumeration.NotificationType;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.iservice.business.ClientServiceAsync;
import org.iabako.shared.iservice.business.SaleServiceAsync;
import org.iabako.shared.iservice.generic.CRUDServiceAsync;
import org.iabako.shared.iservice.generic.SearchServiceAsync;
import org.iabako.shared.iservice.generic.SuggestServiceAsync;
import org.iabako.shared.querydefinition.dto.QueryDefinitionDTO;

/**
 * Created by jose on 03/09/14.
 */
public class SaleSearch extends AbstractSearchPage {

    interface MyUiBinder extends UiBinder<HTMLPanel, SaleSearch> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    @UiField
    HTMLPanel saleSearchPanel;
    @UiField
    HTMLPanel resultsPanel;

    public SaleSearchForm saleSearchForm;

    public SaleSearch() {
        super();

        if (!(this instanceof QuoteSearch)) {

            Button createSale = new Button("pageactionbutton", "sale_make_title");
            page.actionButtons.add(createSale);

            createSale.getButton().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    NavigationController.getInstance().goTo(PageEnum.SaleMake);
                }
            });

            Button quotes = new Button("pageactionbutton", "quote_title");
            page.actionButtons.add(quotes);
            quotes.getButton().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    NavigationController.getInstance().goTo(PageEnum.QuoteSearch);
                }
            });
        }
    }

    @Override
    protected void dataTableRemoveCustomAction() {
        AuthenticateUserProvider.getNotificationPanel().refresh(NotificationType.newPastInstallments, NotificationType.futureInstallments, NotificationType.paymentRequest);
        AuthenticateUserProvider.getTrackingPanel().refresh(TrackingType.saleDelete);
    }

    @Override
    protected SearchServiceAsync getSearchServiceAsync() {
        return SaleServiceAsync.Util.getInstance();
    }

    @Override
    protected CRUDServiceAsync getCRUDServiceAsync() {
        return SaleServiceAsync.Util.getInstance();
    }

    @Override
    protected SuggestServiceAsync getSuggestServiceAsync() {
        return ClientServiceAsync.Util.getInstance();
    }

    @Override
    protected HTMLPanel getResultsPanel() {
        return resultsPanel;
    }

    @Override
    protected HTMLPanel getSearchPanel() {
        return saleSearchPanel;
    }

    @Override
    protected HTMLPanel getContentPanel() {
        return binder.createAndBindUi(this);
    }

    @Override
    protected Composite getSearchForm(DataTable dataTable) {
        if (saleSearchForm == null) {
            saleSearchForm = new SaleSearchForm(new DefaultSuggestHandler(getSuggestServiceAsync()), dataTable);
        }
        return saleSearchForm;
    }

    @Override
    protected PageConfigSearchEnum getSearchPageConfigEnum() {
        return PageConfigSearchEnum.SaleSearch;
    }

    @Override
    protected PageEnum getModificationPage() {
        return PageEnum.SaleGoTo;
    }

    @Override
    protected QueryDefinitionDTO getQueryDefinitionDTO() {
        QueryDefinitionDTO queryDefinitionDTO = new QueryDefinitionDTO();
        queryDefinitionDTO.setAscByDefault(false);
        queryDefinitionDTO.setAsc(false);
        return queryDefinitionDTO;
    }
}
